package com.starblink.web.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import com.blankj.utilcode.util.ProcessUtils
import com.starblink.web.app.databinding.ActivityWebDemoBinding
import com.starblink.web.core.GuangWebView
import com.starblink.web.process.BridgeInvokeDispatcher
import com.starblink.web.util.AndroidBug5497Workaround
import com.starblink.web.util.GuangWebViewPool
import com.starblink.web.util.WebLog

class WebDemoActivity : AppCompatActivity() , GuangWebView.OnWebChangeListener {
    private lateinit var mWebUrl:String
    private lateinit var viewBinding: ActivityWebDemoBinding
    private val mWebView by lazy { GuangWebViewPool.getInstance().getWebView(this,mWebUrl) }
    override fun onCreate(savedInstanceState: Bundle?) {
        viewBinding = ActivityWebDemoBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)

        initData()
        initView()
    }

    private fun initView(){
        //开启调试模式
        mWebView.setDebugEnabled(true)

        val params = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        viewBinding.flContent.addView(mWebView,params)
        mWebView.setLifecycleOwner(this)
        mWebView.setOnWebChangeListener(this)
        mWebView.guangLoadUrl(mWebUrl,true)
        //webView软键盘挡住输入框 会导致页面展示内容不完全
        AndroidBug5497Workaround.assistActivity(viewBinding.flContent,this)
    }
    private fun initData(){
        WebLog.d("CurrentProcess", "WebDemoActivity 当前进程：${ProcessUtils.getCurrentProcessName()}")
        mWebUrl = intent.getStringExtra("mWebUrl") ?: ""
    }

    override fun titleChange(title: String?) {

    }

    override fun progressChange(progress: Int) {

    }

    override fun onInnerLinkChecked() {

    }

    override fun onWebLoadError() {

    }

    override fun onWebLoadStart(url: String) {

    }

    override fun onWebLoadFinish(url: String?) {

    }

    override fun onWebLoadStatus(params: Map<String, String>) {

    }

    override fun showLoadingAnim(show: Boolean?) {

    }
}